package com.zareal.ymcc.web.controller;

import com.zareal.ymcc.service.IBlackListService;
import com.zareal.ymcc.domain.BlackList;
import com.zareal.ymcc.query.BlackListQuery;
import com.zareal.ymcc.result.JSONResult;
import com.zareal.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/blackList")
public class BlackListController {

    @Autowired
    public IBlackListService blackListService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody BlackList blackList) {
        if (blackList.getId() != null) {
            blackListService.updateById(blackList);
        } else {
            blackListService.insert(blackList);
        }
        return JSONResult.success();
    }

    // 短信管理添加黑名单用户
    @RequestMapping(value = "/saveblack", method = RequestMethod.POST)
    public JSONResult saveblack(@RequestBody BlackList blackList) {
        JSONResult jsonResult = blackListService.saveblack(blackList);
        return JSONResult.success(jsonResult);
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        blackListService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(blackListService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(blackListService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody BlackListQuery query) {
        Page<BlackList> page = new Page<BlackList>(query.getPage(), query.getRows());
        page = blackListService.selectPage(page);
        return JSONResult.success(new PageList<BlackList>(page.getTotal(), page.getRecords()));
    }
}
